<?php
/**
 * User: Drery
 * Date: 2015/11/28
 * Time: 19:04
 */

namespace backend\controllers;
use backend\components\MyCaptchaAction;
use backend\models\AdminNoticeSearch;
use backend\models\Profile;
use common\models\Notice;
use common\models\Study;
use Yii;
use backend\models\LoginForm;
use yii\web\NotFoundHttpException;

class PublicController extends InitController
{
    public function actions()
    {
        $actions = parent::actions();
        $actions['captcha'] = [
            'class' => MyCaptchaAction::className(),
            'height' => 34,
        ];
        return $actions;
    }

    public function actionLogin()
    {
        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->redirect('/');
        }
        return $this->renderPartial('login',['model'=>$model]);
    }

    public function actionProfile()
    {
        $model = new Profile();
        if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
            if ($model->execute()) {
                Yii::$app->user->logout();
                return $this->redirect('/');
            }
        }
        return $this->render('profile',[
            'model' => $model
        ]);
    }

    public function actionLogout()
    {
        Yii::$app->user->logout();
        return $this->redirect('login');
    }

    public function actionIp()
    {
        $ip = Yii::$app->request->get('ip');
        $long = ip2long($ip);
        /* @var \yii\redis\connection $Redis*/
        $Redis = Yii::$app->redis;
        if ($location = $Redis->hget(REDIS_KEY_IPLOCATIONS,$long)) {
            return $location;
        }
        $arr = json_decode ( file_get_contents ( 'http://ip.taobao.com/service/getIpInfo.php?ip=' . $ip ), true );
        if ($arr ['code'] == 1) {
            $location = $ip;
        } else {
            $data = $arr ['data'];
            if ($data ['country'] != '中国') {
                $location = $data ['country'];
            } else {
                $location = $data ['region'] . $data ['city'] . $data ['county'] . ' ' . $data ['isp'];
            }
        }
        $Redis->hset(REDIS_KEY_IPLOCATIONS,$long,$location);
        return $location;
    }

    public function actionInit()
    {
        $filterModel = new AdminNoticeSearch();
        $dataProvider = $filterModel->search(Yii::$app->request->get());
        $dataProvider->pagination = ['pageSize' => 20];
        $dataProvider->sort = false;

        return $this->render('init', [
            'dataProvider' => $dataProvider,
            'filterModel' => $filterModel
        ]);
    }

    public function actionNotice()
    {
        $id = Yii::$app->request->get('id');
        $model = Notice::find()->where(['id' => $id, 'is_deleted' => 0])->one();
        if (!$model)
            throw new NotFoundHttpException();
        return $this->renderPartial('notice', [
            'model' => $model
        ]);
    }

    public function actionStudyDetail()
    {
        $id = Yii::$app->request->get('id');
        $model = Study::find()->where(['id' => $id, 'is_deleted' => 0])->one();
        if (!$model)
            throw new NotFoundHttpException();
        return $this->renderPartial('study', [
            'model' => $model
        ]);
    }

    /*public function actionStatTotal()
    {
        $province = '河南省';
        $city = '焦作市';
        $statList = Statistics::find()
            ->where(['province' => $province, 'city' => $city])
            ->with([
                'companys' => function (ActiveQuery $query) use ($province, $city) {
                    $query->select('region')->where(['province' => $province, 'city' => $city]);
                },
                'users' => function (ActiveQuery $query) use ($province, $city)  {
                    $query->primaryModel = new Statistics();
                    $query->select('license_plate')->via('vehicles', function(ActiveQuery $query) use ($province, $city) {
                        $query->select('region, license_plate')->where(['province' => $province, 'city' => $city]);
                    });
                }
            ])
            ->orderBy(['arrange' => SORT_ASC])
            ->all();
        return $this->renderPartial('stattotal', [
            'list' => $statList
        ]);
    }

    public function actionStatCompany()
    {
        $province = '河南省';
        $city = '焦作市';
        $statList = Statistics::find()
            ->where(['province' => $province, 'city' => $city])
            ->with([
                'companys' => function (ActiveQuery $query) use ($province, $city) {
                    $query->where(['province' => $province, 'city' => $city])->with([
                        'users' => function (ActiveQuery $query) use ($province, $city)  {
                            $query->primaryModel = new Company();
                            $query->select('license_plate')->via('vehicles', function(ActiveQuery $query) use ($province, $city) {
                                $query->select('region, license_plate, company')->where(['province' => $province, 'city' => $city]);
                            });
                        }
                    ]);
                }
            ])
            ->orderBy(['arrange' => SORT_ASC])
            ->all();
        return $this->renderPartial('statcompany', [
            'list' => $statList
        ]);
    }

    public function actionStatHtype()
    {
        $province = '河南省';
        $city = '焦作市';
        $statList = Statistics::find()
            ->where(['province' => $province, 'city' => $city])
            ->with(['vehicles' => function (ActiveQuery $query) {
                $query->where(['province' => '河南省', 'city' => '焦作市']);
            }])
            ->orderBy(['arrange' => SORT_ASC])
            ->all();
        return $this->renderPartial('stathtype', [
            'list' => $statList
        ]);
    }

    public function actionStatStudy()
    {
        $province = '河南省';
        $city = '焦作市';
        $statList = Statistics::find()
            ->where(['province' => $province, 'city' => $city])
            ->with([
                'companys' => function (ActiveQuery $query) use ($province, $city) {
                    $query->select('region')->where(['province' => $province, 'city' => $city]);
                },
                'users' => function (ActiveQuery $query) use ($province, $city)  {
                    $query->primaryModel = new Statistics();
                    $query->select('license_plate')->with('userExam')->via('vehicles', function(ActiveQuery $query) use ($province, $city) {
                        $query->select('region, license_plate')->where(['province' => $province, 'city' => $city]);
                    });
                }
            ])
            ->orderBy(['arrange' => SORT_ASC])
            ->all();
        return $this->renderPartial('statstudy', [
            'list' => $statList
        ]);
    }

    public function actionStatExam()
    {
        $province = '河南省';
        $city = '焦作市';
        $statList = Statistics::find()
            ->where(['province' => $province, 'city' => $city])
            ->with([
                'companys' => function (ActiveQuery $query) use ($province, $city) {
                    $query->select('region')->where(['province' => $province, 'city' => $city]);
                },
                'users' => function (ActiveQuery $query) use ($province, $city)  {
                    $query->primaryModel = new Statistics();
                    $query->select('license_plate')->with('userExamine')->via('vehicles', function(ActiveQuery $query) use ($province, $city) {
                        $query->select('region, license_plate')->where(['province' => $province, 'city' => $city]);
                    });
                }
            ])
            ->orderBy(['arrange' => SORT_ASC])
            ->all();
        return $this->renderPartial('statexam', [
            'list' => $statList
        ]);
    }*/
}